package servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.StringConstants;

import database.DatabaseOperations;

/**
 * Servlet implementation class EditJobState
 */
@WebServlet("/EditJobState")
public class EditJobState extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EditJobState() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	public void doPost(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		DatabaseOperations databaseOperations = new DatabaseOperations();
		try {
			
			int orderId = Integer.parseInt(req.getParameter("orderId_editjobstate"));
			String jobState = req.getParameter("jobState_edit");
			if(jobState.compareTo("RESERVED") == 0)
			{
				String transporter = req.getParameter("transporter_name");
				databaseOperations.editJobState(orderId, jobState, transporter);
			}
			else
			{
				databaseOperations.editJobState(orderId, jobState);
			}
		} finally {
			databaseOperations.closeConnection();
		}
		res.sendRedirect(StringConstants.admin_homepage);
		
		//System.out.println("CHANGed JOB STATE");
	}

	
}
